package com.example.lin.ximalaya.adapter.viewholder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lin.ximalaya.R;
import com.example.lin.ximalaya.pojo.HotData;
import com.example.lin.ximalaya.pojo.Type4listItem;
import com.example.lin.ximalaya.utils.NumberUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class Type4ViewHolder extends RecyclerView.ViewHolder {

    private Context context;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.rv_item)
    RecyclerView rvItem;
    private GuessLikeRVAdapter guessLikeRVAdapter;

    @OnClick(R.id.ll_change)
    public void change(View view) {
        Toast.makeText(context, "点击了换一批!", Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.tv_more)
    public void more(View view) {
        Toast.makeText(context, "点击了更多!", Toast.LENGTH_SHORT).show();
    }

    public Type4ViewHolder(Context context, View itemView) {
        super(itemView);
        this.context = context;
        ButterKnife.bind(this, itemView);
        initView();
    }

    private void initView() {
        rvItem.setLayoutManager(new GridLayoutManager(context, 3, LinearLayoutManager.VERTICAL, false));
        guessLikeRVAdapter = new GuessLikeRVAdapter(context);
        rvItem.setAdapter(guessLikeRVAdapter);
    }

    /**
     * 为 ViewHolder 绑定数据
     *
     * @param data 数据对象
     */
    public void setData(HotData.ListBeanX data) {
        List<JsonObject> list = data.getList();
        ArrayList<Type4listItem> listItems = new ArrayList<>();
        for (JsonObject obj : list) {
            Type4listItem type4listItem = new Gson().fromJson(obj, Type4listItem.class);
            listItems.add(type4listItem);
        }
        guessLikeRVAdapter.setListItems(listItems);
    }

    class GuessLikeRVAdapter extends RecyclerView.Adapter<GuessLikeRVAdapter.ItemViewHolder> {

        Context context;

        public GuessLikeRVAdapter(Context context) {
            this.context = context;
        }

        List<Type4listItem> listItems;

        public void setListItems(List<Type4listItem> listItems) {
            this.listItems = listItems;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View layout = LayoutInflater.from(context).inflate(R.layout.view_rv_item_type_4_rv_item, parent, false);
            return new ItemViewHolder(layout);
        }

        @Override
        public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
            Type4listItem type4listItem = listItems.get(position);
            holder.tvTitle.setText(type4listItem.getTitle());
            holder.tvNum.setText(NumberUtils.number2String(type4listItem.getPlaysCount()));
            Glide.with(context).load(type4listItem.getPic()).into(holder.ivIcon);
        }

        @Override
        public int getItemCount() {
            return listItems == null ? 0 : listItems.size();
        }

        class ItemViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_icon)
            ImageView ivIcon;
            @BindView(R.id.tv_num)
            TextView tvNum;
            @BindView(R.id.rv_icon)
            CardView rvIcon;
            @BindView(R.id.tv_title)
            TextView tvTitle;

            ItemViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);

                //确定 CardView 的宽高
                int widthPixels = context.getResources().getDisplayMetrics().widthPixels;
                float spacing = context.getResources().getDimension(R.dimen.dimens_5dp);
                int cardViewWidth = (int) ((widthPixels - 8 * spacing) / 3);

                ViewGroup.LayoutParams lp = rvIcon.getLayoutParams();
                lp.width = cardViewWidth;
                lp.height = cardViewWidth;
                rvIcon.setLayoutParams(lp);

            }
        }
    }

}
